package thread.Test;

public class YieldTest {
	public static void main(String[]args){
		ThreadB t1=new ThreadB("t1");
		ThreadB t2=new ThreadB("t2");
		t1.start();
		t2.start();

	}
}

class ThreadB extends Thread {
	public ThreadB(String name) {
		super(name);
	}

	public synchronized void run() {
		for (int i = 0; i < 10; i++) {
			System.out.printf("%s [%d]:%d\n", this.getName(),
					this.getPriority(), i);
			if (i%4 == 0){
				Thread.yield();//使线程从执行状态变为就绪状态，从而让其它具有相同优先级的等待线程获取执行权，但是并不保证
			}
		}
	}
};
/*
 * t2 [5]:0
t1 [5]:0
t2 [5]:1
t1 [5]:1
t2 [5]:2
t1 [5]:2
t2 [5]:3
t1 [5]:3
t2 [5]:4
t1 [5]:4
t2 [5]:5
t1 [5]:5
t1 [5]:6
t1 [5]:7
t2 [5]:6
t1 [5]:8
t2 [5]:7
t2 [5]:8
t1 [5]:9
t2 [5]:9
 * */
